//
//  HSClassTableController.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/9/30.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

class HSClassTableController: UIViewController {

    //标签宽度
    let lableW:CGFloat = screenW/8
    let lableH:CGFloat = 44
    
    //数据源数组
    var dataArray: [HSClassTableModel] = []
    
    //全局的集合视图属性
    var collectionView:UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.white
        setupTopUI()
        addcollectionView()
        setupWeekView()
        setupLeftClassTimeView()
        loadData()
        }
    
    func loadData() {
        //接口地址
        let url = "ZH-schoolname-S-curriculum"
        //参数
        let key = userInfo["key"] as! String
        let param = ["key":key]
        //请求数据
        HSNetworkTools.shared.downloadData(url: url, parameters: param) {[weak self] (data, error) in
            //判断网络是否有问题
            guard let data = data as? NSDictionary else {
                print("网络异常")
                return
            }
            ///判断返回的数据是否正常
            switch data["result"] as! Int {
            case 0:
                //获得数据数组
                let dataArr = data["data"] as! NSArray
                //print(dataArr)
                //循环获得每天的字典
                for dict in dataArr {
                    let cDict = dict as? NSDictionary
                    //获得字典的所有keys,并按照小--大的排序
                    let keys = (cDict?.allKeys as! [String]).sorted { $0 < $1 }
                    //循环获得课程字典
                    for key in keys {
                        //print(key)
                        //获得字典
                        let d = cDict?[key] as! [String: Any]
                        //字典转模型
                        let model = HSClassTableModel(dict: d)
                        //添加到数据源数组中
                        self?.dataArray.append(model)
                        //添加空白对象，如果为每天的第四节课，自动添加一个空的对象
                        if key == "c4"{
                            self?.dataArray.append(HSClassTableModel(dict: [:]))
                    }
                }
            }
                //到这里就表示所有数据全部处理完成
                //刷新视图
                self?.collectionView.reloadData()
            case 1:
                print("参数错误")
            default:
                break
            }
        }
    }
}

//设置UI
extension HSClassTableController{
    //顶部导航条
    func setupTopUI() {
        //模拟一个假导航栏
        let navVc = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
        navVc.backgroundColor = UIColor().colorWithHex(0x009ee7)
        view.addSubview(navVc)
        //返回按钮
        let back = UIButton(frame: CGRect(x: 8, y: 16, width: 70, height: 48))
        back.setImage(UIImage(named:"返回按钮"), for: .normal)
        back.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
        navVc.addSubview(back)
        //文字
        let label = UILabel(frame: CGRect(x: 60, y: 16, width: 150, height: 48))
        label.text = "关于我们"
        label.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        label.font = UIFont.systemFont(ofSize: 20)
        
        navVc.addSubview(label)
        
        
    }
    //返回按钮监听方法
    func backBtnClicked(){
        dismiss(animated: true, completion: nil)
    }
    //周视图
    func setupWeekView() {
        //创建周数组
        let weekTitle = ["","一","二","三","四","五","六","日"] as NSArray
        //循环添加视图
        weekTitle.enumerateObjects( { (title, i,_ ) in
            //初始化
            let lable = UILabel(frame: CGRect(x: lableW * CGFloat(i), y: 64, width: lableW, height: lableH))
            lable.text = title as? String
            lable.textAlignment = .center
            lable.textColor = UIColor().colorWithHex(0x00a0e9)
            lable.font = UIFont.systemFont(ofSize: 13)
            view.addSubview(lable)
        })
    }
    //左边课时视图
    func setupLeftClassTimeView() {
        //循环
        for i in 1..<10{
            if i == 5 {
                let btn = UIButton(type: .custom)
                btn.frame = CGRect(x: 0, y: lableH * CGFloat(i) + 64, width: screenW, height: lableH)
                btn.setBackgroundImage(UIImage(named: "curriculum_wuxiu_bg"), for: .normal)
                btn.setTitle("午 休", for: .normal)
                btn.isUserInteractionEnabled = false
                btn.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                view.addSubview(btn)
                
            }else{
                
                let label = UILabel(frame: CGRect(x: 0, y: lableH * CGFloat(i) + 64 , width: lableW, height: lableH))
                if i > 5 {
                    label.text = "\(i - 1)"
                }else{
                    label.text = "\(i)"
                }
                label.textAlignment = .center
                label.font = UIFont.systemFont(ofSize: 13)
                view.addSubview(label)
                //添加线条
                let linV = UIView(frame: CGRect(x: 8, y: lableH * CGFloat(i + 1) + 64, width: screenW - 16, height: 1))
                linV.backgroundColor = UIColor().colorWithHex(0x00a00e9)
                view.addSubview(linV)
            }
        }
    }
    //中间的集合视图
    func addcollectionView() {
        //设置布局对象
        let flowLayout = UICollectionViewFlowLayout()
        //设置大小和间距
        flowLayout.itemSize = CGSize(width: lableW, height: lableH)
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
        //滚动方向
        flowLayout.scrollDirection = .horizontal
        
        let rect = CGRect(x: lableW,
                          y: lableH + 64,
                          width: lableW * 7,
                          height: lableH * 9)
        collectionView = UICollectionView(frame: rect, collectionViewLayout: flowLayout)
        collectionView.backgroundColor = UIColor.white
        //设置代理和数据源
        collectionView.dataSource = self
        collectionView.register(HSClassTableCell.self, forCellWithReuseIdentifier: "classTabelCell")
        
        view.addSubview(collectionView)
    }

}

//集合视图的代理源方法
extension HSClassTableController:UICollectionViewDataSource{
    //格子数量
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    //格子视图
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "classTabelCell", for: indexPath) as? HSClassTableCell
        
        cell?.title?.text = dataArray[indexPath.item].courseName
    
        return cell!
    }
    
    
}















